﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Configuration;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;
using Hangjing.WebCommon;

/// <summary>
/// 小程序提交订单
/// </summary>
public partial class APP_Android_submitorder_buildorder : APIPageBase
{
    Custorder orderdal = new Custorder();
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        string ret = " {\"orderstate\":\"2\" } ";
        //ordermodel={ "OrderRcver": "测试", "OrderComm": "18758580307", "AddressText": "杭州市江干区学正街547号金沙居1239", "OrderAttach": "[东][1份餐]", "OrderSums": 100, "SendTime": "2015-06-17T09:50:00", "P2Sign": "东" }
        string jsonstring = Request["ordermodel"];

        Hangjing.AppLog.AppLog.Info("jsonstring:" + jsonstring);


        string lat1 = WebUtility.InputText(Request["lat1"]);
        string lng1 = WebUtility.InputText(Request["lng1"]);
        string lat2 = WebUtility.InputText(Request["lat2"]);
        string lng2 = WebUtility.InputText(Request["lng2"]);


        CustorderInfo infoaddress = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<CustorderInfo>(jsonstring);

        infoaddress.tempcode = Request["openid"];
        infoaddress.CustomerName = "";

        if (infoaddress.fromweb == null)
        {
            infoaddress.fromweb = "7";
        }
        infoaddress.ReveVar1 = "0";
        infoaddress.ReveVar2 = "{'ulat':'" + lat1 + "','ulng':'" + lng1 + "','slat':'" + lat2 + "','slng':'" + lng2 + "'}";
        infoaddress.OrderSums = 0;
        infoaddress.Advance = 0;
        infoaddress.shoptip = 0;
        infoaddress.OldPrice = infoaddress.OrderSums;

        infoaddress.TogoId = 0;
        infoaddress.P2Sign = "";
        infoaddress.paytime = Convert.ToDateTime("1970-1-1");
        infoaddress.IsShopSet = 0;
        infoaddress.ReveDate1 = DateTime.Now;
        infoaddress.cardids = "";
        infoaddress.Commission = 0;
        infoaddress.settlecycle = 0;

        infoaddress.Mergeorderremark = "";
        infoaddress.SystemUserId = 0;
        infoaddress.InUse = "";
        infoaddress.deliverheaderid = 0;
        infoaddress.OldStatus = 0;


        int cityid = 17;
        string cityname = "福清市";


        foreach (var item in SectionProxyData.GetCityList())
        {
            if (infoaddress.AddressText.IndexOf(item.cname) >= 0)
            {
                cityid = item.cid;
                cityname = item.cname;
            }
        }

        infoaddress.cityid = cityid;
        infoaddress.paystate = 0;
        infoaddress.paytime = Convert.ToDateTime("1970-1-1");
        infoaddress.paymoney = 0;
        if (infoaddress.paymode == 0) //未指定，按微信
        {
            infoaddress.paymode = 5;
        }
        UserShareIncomeDal userShareIncomeDal = new UserShareIncomeDal();
        if (infoaddress.paymode == 3) //判断余额
        {
            user_share_income user_Share_Income = userShareIncomeDal.getUserLimit(infoaddress.UserId);
            if (user_Share_Income.user_limit < infoaddress.shopdiscountmoney)
            {
                rs.msg = "余额不足";
                sendToClient();
                Response.End();
                return;
            }
        }

        if (orderdal.AddTBOrder(infoaddress) > 0)
        {
            //EAddressInfo addrmodel = new EAddressInfo();
            //addrmodel.Receiver = infoaddress.OrderRcver;
            //addrmodel.Address = infoaddress.AddressText;
            //addrmodel.Phone = "";

            //addrmodel.BuildingID = 0;
            //addrmodel.Mobilephone = infoaddress.OrderComm;
            //addrmodel.UserID = 0;
            //addrmodel.AddTime = DateTime.Now;
            //addrmodel.Pri = 0;
            //addrmodel.Lat = ulat;
            //addrmodel.Lng = ulng;
            //int userid = new EAddress().SaveUserAndAddress(addrmodel);

            if (infoaddress.paymode == 3) //余额支付
            {
                user_share_income_log log = new user_share_income_log();
                log.c_t = DateTime.Now;
                log.u_t = DateTime.Now;
                log.source = 2;
                log.msg = "支付订单："+ infoaddress.orderid;
                log.user_limit = 0;
                log.user_id = infoaddress.UserId;
                log.from_user_id = 0;
                log.change_limit = infoaddress.shopdiscountmoney;

                apiResultInfo apiResult = userShareIncomeDal.changeLimit(log, LimitOperation.sub);
                if (apiResult.state == 0)
                {
                    rs.msg = apiResult.msg;
                    sendToClient();
                    Response.End();
                    return;
                }
                else
                {
                    CustorderInfo info = orderdal.GetModel(infoaddress.orderid);
                    info.orderid = info.orderid;
                    info.paymode = infoaddress.paymode;
                    info.paymoney = infoaddress.shopdiscountmoney;
                    info.paystate = 1;
                    info.paytime = DateTime.Now;
                    orderdal.UpdatePayState(info);

                    new OrderStep().Add(new OrderStepInfo()
                    {
                        stepcode = 10,
                        title = "支付成功",
                        subtitle = "",
                        addtime = DateTime.Now,
                        orderid = info.orderid,
                        revevar1 = "",
                        revevar2 = ""

                    });
                }

            }

            ret = "{\"state\":\"1\",\"paystate\":\"" + infoaddress.paystate + "\",\"paymoney\":\"" + infoaddress.shopdiscountmoney + "\",\"orderid\":\"" + infoaddress.orderid + "\",\"cityid\":\"" + cityid + "\"}";
        }
        else
        {
            rs.msg = "服务器繁忙，请稍候再试";
            sendToClient();
            Response.End();
            return;
        }

        Response.Write(ret);
        Response.End();

    }
}
